package com.coolham.switter;

import com.coolham.common.util.DeviceInfoUtil;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Build;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceClickListener;
import android.telephony.gsm.GsmCellLocation;
import android.util.DisplayMetrics;
import android.util.Log;

/**
 *  Dispaly software version and hardware information
 *  
 * @author Jeff
 *
 */
public class DeviceInfoActivity extends PreferenceActivity {
	private final static String TAG = "DeviceInfoActivity";
	
	private Context mContext;
	private DeviceInfoUtil mDeviceInfoUitl;
	
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		mDeviceInfoUitl = new DeviceInfoUtil(mContext);
		
		setTitle("About Phone");
		addPreferencesFromResource(R.xml.deviceinfo_pref);
		
		initView();
	}

	private void initView() {
		setPackageVersion();
		setIMEI();
		setIMSI();
		setDisplay();
		setLocation();
		setBuild();
	}

	private void setDisplay() {
		DisplayMetrics dm = new DisplayMetrics();  
		getWindowManager().getDefaultDisplay().getMetrics(dm); 
		
		int width = dm.widthPixels;
		int height = dm.heightPixels;
		float density = dm.density;
		float xdpi = dm.xdpi;
		float ydpi = dm.ydpi;		
		
		String display = String.valueOf(width) + "x" + String.valueOf(height);
		findPreference("phone_info_screen_size").setSummary(display);
		
	}

	private void setLocation() {
		GsmCellLocation location = mDeviceInfoUitl.getCellLocation();
		int cid = location.getCid();
		int lac = location.getLac();
		findPreference("phone_info_location_cid").setSummary(String.valueOf(cid));
		findPreference("phone_info_location_lac").setSummary(String.valueOf(lac));
	}

	private void setPackageVersion() {
		PackageInfo pi = mDeviceInfoUitl.getPackageInfo();
		String version = "name=" + pi.versionName + " code=" + pi.versionCode;
		findPreference("package_info").setSummary(version);
		
		findPreference("package_manual_update").setOnPreferenceClickListener(new OnPreferenceClickListener(){
			public boolean onPreferenceClick(Preference preference) {
				return false;
			}
		});
	}

	private void setIMSI() {
		String imsi = mDeviceInfoUitl.getIMSI();
		String number = mDeviceInfoUitl.getLine1Number();
		Log.d(TAG, "number=" + number);
		
		if(number != null && number.length()>0)
			imsi += "(" + number + ")";
		findPreference("phone_info_imsi").setSummary(imsi);
		
	}

	private void setIMEI() {
		String imei = mDeviceInfoUitl.getIMEI();
		findPreference("phone_info_imei").setSummary(imei);
		
	}
	
	private void setBuild() {
		findPreference("phone_info_build_device").setSummary(Build.DEVICE);
		findPreference("phone_info_build_display").setSummary(Build.DISPLAY);
		findPreference("phone_info_build_manufacturer").setSummary(Build.MANUFACTURER);
		findPreference("phone_info_build_model").setSummary(Build.MODEL);
		findPreference("phone_info_build_product").setSummary(Build.PRODUCT);
	}

}
